/*
 * Activador del servicio OCP 
 */

package ocp.service.impl;

import ocp.management.*;

import jena.tests.JenaTest;
import java.util.Hashtable;
import jena.tests.TestPersistentOntology;
import jena.tests.Tutorial01;
import jena.tests.Tutorial03;
import jena.tests.Tutorial04;
import jena.tests.Tutorial05;
import jena.tests.Tutorial06;
import jena.tests.Tutorial07;
import jena.tests.Tutorial08;
import jena.tests.Tutorial09;
import jena.tests.Tutorial10;
import jena.tests.Tutorial11;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
import org.osgi.framework.BundleActivator;

import ocp.service.*;
import ocp.util.Log;
import org.osgi.framework.ServiceRegistration;



/**
 *
 * @author Paco López Mármol
 */
public class Activator implements BundleActivator {
    
    private static BundleContext bc = null;
    private ContextServiceImpl cs;
    private ContextManagement cm;
    OntologyManagement om;
    

    public void start(BundleContext bc) throws Exception {
        
        System.out.println("OCP: " + bc.getBundle().getHeaders().get(
                        Constants.BUNDLE_NAME)
                        + " Iniciado. Consulta log para detalles.");
        Activator.bc = bc;

        /*
        // Variables del entorno OSGi
        System.out.println("OCP: Procesador:" 
                        + bc.getProperty(Constants.FRAMEWORK_PROCESSOR) );
        
        // Propiedades(Headers) del fichero de Manifiesto
        System.out.println("OCP: Prueba2:" 
                        + bc.getBundle().getHeaders().get("X-COMMENT") );
        
        // 
        System.out.println("OCP: Prueba3:" 
                        + bc.getProperty("org.knopflerfish.ocp.texto" ));
        */
        
        //dbDriver , dbUrl , dbUser , dbPassword , dbName

        Log.init( bc );
        Log.logInfo( "OCP: Parametros de acceso a BBDD: " 
                        
                + bc.getProperty("org.ocp.dbDriver")
                + " " + bc.getProperty("org.ocp.dbUrl")
                + " " + bc.getProperty("org.ocp.dbUser")
                + " " + bc.getProperty("org.ocp.dbPassword")
                + " " + bc.getProperty("org.ocp.dbName")
                );

        // Inicializamos el sistema persistente y de inferencia
        try  {
           om = OntologyManagement.getInstance(
                bc.getProperty("org.ocp.dbDriver")
                ,bc.getProperty("org.ocp.dbUrl")
                ,bc.getProperty("org.ocp.dbUser")
                ,bc.getProperty("org.ocp.dbPassword")
                ,bc.getProperty("org.ocp.dbName")
                );
        }catch( Exception e)  {
           om = null; 
           Log.logError( "OCP: No se pudo conectar con la base de datos" , e);        
           return;
        }
        
        
        // Inicializamos el objeto que tendrá/comunicará el/con almacen de contextos
        cm = ContextManagement.getInstance(om);
        
        cs = new ContextServiceImpl( bc , cm );
        ServiceRegistration csRegistration = bc.registerService(
            ContextService.class.getName(), cs, new Hashtable());
        cs.open();
        //System.out.println("OCP: ContextService registrado");        
        
        
        // Jena test ***********************************************************
        //JenaTest jenaTest = new JenaTest();
        //jenaTest.testDescribeClass(null);
        
        //TestPersistentOntology t = new TestPersistentOntology();
        //t.test();

        /*
        Tutorial04 t = new Tutorial04();
        t.test();
        System.out.println("*********************************************");
        Tutorial05 t2 = new Tutorial05();
        t2.test();
        System.out.println("*********************************************");
        Tutorial06 t6 = new Tutorial06();
        t6.test();
        System.out.println("*********************************************");
        Tutorial07 t7 = new Tutorial07();
        t7.test();
        System.out.println("*********************************************");
        Tutorial08 t8 = new Tutorial08();
        t8.test();
        System.out.println("*********************************************");
        Tutorial09 t9 = new Tutorial09();
        t9.test();
        System.out.println("*********************************************");
        Tutorial10 t10 = new Tutorial10();
        t10.test();
        System.out.println("*********************************************");
        Tutorial11 t11 = new Tutorial11();
        t11.test();
        //**********************************************************************
        */
    }

    public void stop(BundleContext bc) throws Exception {
        /*
        System.out.println("OCP: " + bc.getBundle().getHeaders().get(
                        Constants.BUNDLE_NAME)
                        + " cerrando...");
         */

        if( om == null ) return;
        cm.terminate();
        Activator.bc = null; // no hay necesidad de "desregistrar" los  servicios
        // el framework lo hace automaticamente
    }
    
}
